{include file='public/header' /}
<div class="layuimini-container">
    <form id="modelUserForm" lay-filter="modelUserForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-card-header layuiadmin-card-header-auto">
            <h2>添加广告信息</h2>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">广告位置</label>
            <div class="layui-input-inline">
                <select name="position_id" lay-verify="required">
                    <option value=""></option>
                    {foreach $data as $v}
                    <option value="{$v.id}">{$v.name}</option>
                    {/foreach}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="" class="layui-form-label">广告标题</label>
            <div class="layui-input-inline">
                <input type="text" name="title" value="" lay-verify="required" placeholder="请输入标题" class="layui-input" >
            </div>
        </div>



        <div class="layui-form-item">
            <label for="" class="layui-form-label">主图</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-sm uploadPic"><i class="layui-icon">&#xe67c;</i>图片上传</button>
                    <div class="layui-upload-list" >
                        <ul class="layui-upload-box layui-clear">
                            {if condition='isset($row.thumb)'}
                            <li><img style="width:100px;height:60px" src="{$row.thumb}" /></li>
                            {/if}
                        </ul>
                        <input type="hidden" name="thumb" class="layui-upload-input" value="{{ $row.thumb??'' }}">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="" class="layui-form-label">排序</label>
            <div class="layui-input-inline">
                <input type="number" name="sort" value="" lay-verify="required|number" placeholder="请输入数字" class="layui-input" >
            </div>
        </div>


        <div class="layui-form-item">
            <label for="" class="layui-form-label">类型</label>
            <div class="layui-input-inline">
                <select name="type" lay-verify="required">
                    <option value="">请选择</option>
                    <option value="1">机构</option>
                    <option value="2">资讯</option>
                    <option value="3">网上课程</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="" class="layui-form-label">绑定跳转ID</label>
            <div class="layui-input-inline">
                <input type="number" name="bind_id" value="" lay-verify="required|number" placeholder="请输入数字" class="layui-input" >
            </div>
        </div>


        <div class="layui-form-item text-center">
            <button class="layui-btn layui-btn-primary" type="button" onclick="close_window()" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitUser" lay-submit>保存</button>
        </div>
    </form>
</div>
{include file='public/footer' /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;


        var url = '{:__url("banner.advert_msg/add")}';


        // 表单提交事件
        form.on('submit(modelSubmitUser)', function (data) {
            layer.load(2);
            console.log(data);
            $.post(url, data.field, function (res) {

                layer.closeAll('loading');
                if (res.code == 1) {
                    layer.msg(res.msg, {icon: 1,time: 1000},function(index){
                        window.parent.location.reload();
                    });
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false;
        });
    });
    function close_window(){
        window.parent.location.reload();
    }
</script>
<script>
    layui.use(['upload','layer','element','form'],function () {
        var $ = layui.jquery;
        var upload = layui.upload;

        //普通图片上传
        $(".uploadPic").each(function (index,elem) {
            upload.render({
                elem: $(elem)
                ,url: '{:__url("admin/upload/upload")}'
                ,multiple: false
                ,data:{"_token":"{{ csrf_token() }}"}
                ,done: function(res){
                    //如果上传失败
                    if(res.code == 0){
                        layer.msg(res.msg,{icon:1,time:1000},function () {
                            $(elem).parent('.layui-upload').find('.layui-upload-box').html('<li><img style="width:100px;height:60px" src="'+res.url+'" /></li>');
                            $(elem).parent('.layui-upload').find('.layui-upload-input').val(res.url);
                        })
                    }else {
                        layer.msg(res.msg,{icon:2,time:1000})
                    }
                }
            });
        })
    })
    function asd(a){
        var formData = new FormData();
        formData.append('file',$(a).prev()[0].files[0]);
        $.ajax({
            url: '{:__url("admin/upload/upload")}',
            dataType:'json',
            type:'POST',
            async: false,
            data : formData,
            processData : false, // 使数据不做处理
            contentType : false, // 不要设置Content-Type请求头
            success: function(data){
                if (data.code == 0) {
                    $(a).next().attr('value',data.url);

                    $(a).siblings("img").attr('src',data.url);

                    layer.msg('上传成功',{icon:1,time:1000});
                }else{
                    layer.msg(data.msg,{icon:2,time:1000});
                }

            },
            error:function(data){
                layer.msg(data.msg);
            }
        });
    }
    function close_window(){
        window.parent.location.reload();
    }
</script>
<link href="/baidu-editor/themes/default/css/umeditor.min.css" type="text/css" rel="stylesheet">
<script src="/baidu-editor/third-party/jquery.min.js"></script>
<!-- 配置文件 -->
<script type="text/javascript" src="/baidu-editor/umeditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/baidu-editor/umeditor.min.js"></script>
<!-- 实例化编辑器 -->
